﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Gao.Poker.Domain;
using Gao.Poker.Services;

namespace Gao.Poker.ViewModel
{
    public class GameViewModel : BaseViewModel
    {
        private Competition _Competition;
        private List<Competition> _Competitions;
        private List<Competition> _AllCompetitions;
        private List<MemberSeat> _members;
        private List<Tables> _tableses;
        private ObservableCollection<Bouns> _bounses;
        private List<Ranking> _rankings;
        private int _pageIndex;
        private int _allCompetitionTotal;

        public String CompetitionName { get; set; }
       

        public GameViewModel()
        {
            CompetitionName = String.Empty;
            if (_rankings == null) _rankings = new List<Ranking>();
        }

        public Competition Competition
        {
            get { return _Competition ?? (_Competition = new Competition()); }
            set
            {
                _Competition = value;

                //_rankings.Clear();
                //if (Competition != null && Competition.ID > 0)
                //{
                //    var members = Members;
                //    foreach (var memberSeat in members)
                //    {
                //        //排除出局的人
                //        if (memberSeat.OutState == 1) continue;
                //        var ranking = new Ranking { CardNo = memberSeat.CardNo, Name = memberSeat.CardNo, Rank = memberSeat.Rank, CompetitionID = Competition.ID };
                //        _rankings.Add(ranking);
                //    }
                //}
                ////////////////////
                if (_bounses == null) _bounses = new ObservableCollection<Bouns>();
                _bounses.Clear();
                if (Competition != null && Competition.ID > 0)
                {
                    foreach (var item in new BounsService().GetBounses(Competition.ID))
                    {
                        _bounses.Add(item);
                    }
                }
                if (_bounses.Count == 0)
                {
                    int index = 1;
                    while (index < 9)
                    {
                        _bounses.Add(new Bouns() { Rank = index });
                        index++;
                    }
                }
                

                OnPropertyChanged("Competition");
                OnPropertyChanged("Members");
                OnPropertyChanged("Tableses");
                OnPropertyChanged("Bounses");
                OnPropertyChanged("Rankings");
            }
        }
        /// <summary>
        /// 更新还剩余的比赛人数
        /// </summary>
        public void UpdateRanking()
        {
            _rankings.Clear();
            if (Competition != null && Competition.ID > 0)
            {
                var members = Members;
                foreach (var memberSeat in members)
                {
                    //排除出局的人
                    if (memberSeat.OutState == 1) continue;
                    var ranking = new Ranking { CardNo = memberSeat.CardNo, Name = memberSeat.CardNo, Rank = memberSeat.Rank, CompetitionID = Competition.ID };
                    _rankings.Add(ranking);
                }
            }
        }

        public List<Competition> Competitions
        {
            get
            {
                _Competitions = new CompetitionService().SearchCompetition();
                return _Competitions;
            }
            set { _Competitions = value; }
        }

        /// <summary>
        /// 已经报名的人
        /// </summary>
        public List<MemberSeat> Members
        {
            get
            {
                if (Competition == null || Competition.ID <= 0) return null;
                _members = new MemberService().GetMemberCompetitionID(Competition.ID);
                return _members;
            }
            set { _members = value; }
        }

        public List<Tables> Tableses
        {
            get
            {
                _tableses = new TablesService().GetTables(Competition.ID);
                return _tableses;
            }
        }

        public ObservableCollection<Bouns> Bounses
        {
            get
            {
                return _bounses;
            }
            set { _bounses = value; }
        }

        public List<Ranking> Rankings
        {
            get
            {
                return _rankings;
            }
            set { _rankings = value; }
        }

        public List<Competition> AllCompetitions
        {
            get
            {
                _AllCompetitions = CompetitionService.Instance().GetAllCompetition(CompetitionName, PageIndex, 20);
                OnPropertyChanged("AllCompetitionTotal");
                return _AllCompetitions;
            }
            set { _AllCompetitions = value; }
        }

        public int AllCompetitionTotal
        {
            get
            {
                _allCompetitionTotal = CompetitionService.Instance().GetAllCompetitionTotal(CompetitionName);
                return _allCompetitionTotal;
            }
        }

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                OnPropertyChanged("AllCompetitions");
            }
        }
    }
}
